/**
 * 
 */
package com.metrology.app.domain;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

import org.apache.commons.beanutils.PropertyUtils;

/**
 * @author justin.chen
 * 
 */
public class DomainObject implements Serializable {
	
	public static final String FIELD_CREATE_DATETIME = "create_time";
	
	public static final String FIELD_UPDATE_DATETIME = "update_time";

	//UUID_SEPERATOR
	private static final String UUID_SEPERATOR = "-";

	private static final long serialVersionUID = 6208129852831190135L;
	
	private boolean isUpdated = false;
	
	private boolean isNewWithId = false;

	public static final Long QA_ONLINE_SYSTEM = 1L;
	
	protected Long id;

	// 创建人
	protected Long createUserId;

	// 创建时间
	protected Date createDateTime;

	// 修改人
	protected Long updateUserId;

	// 修改时间
	protected Date updateDateTime;

	// 删除标志
	protected Boolean deleteFlag = Boolean.FALSE;
	
	protected boolean cacheFlushable;
	
	protected boolean autoGenId;
	
	public DomainObject(){}
	
	public DomainObject(Long uuid){
		this.id = uuid;
	}
	
	public void setUpdateOperation(Long curUserId) {
		setUpdateDateTime(new Date());
		setUpdateUserId(curUserId);
		this.isUpdated = true;
	}

	public void setDeleteOperation(Long curUserId) {
		Date date = new Date();
		setUpdateDateTime(date);
		setUpdateUserId(curUserId);
		setDeleteFlag(true);
		this.isUpdated = true;
	}

	public void setInsertOperation(Long curUserId) {
		Date date = new Date();
		setUpdateDateTime(date);
		setCreateDateTime(date);
		setUpdateUserId(curUserId);
		setCreateUserId(curUserId);
		setDeleteFlag(false);
		this.isUpdated = true;
	}
	
	public void setId(Long id){
		this.id = id;
	}

	/**
	 * @param uuid
	 * the uuid to set
	 */
	public void setUuid(Long uuid) {
		this.id = uuid;
	}
	
	public Long getId(){
		return this.id;
	}

	/**
	 * @return the uuid
	 */
	public Long getUuid() {
		return this.id;
	}
	
	public String getDisUuid() {
		if(this.id == null){
			return "";
		}
		return this.id.toString();
	}

	/**  
	 * createUserId  
	 *  
	 * @return  the createUserId 
	 *
	 */
	public Long getCreateUserId() {
		return createUserId;
	}

	/**  
	 * @param createUserId the createUserId to set  
	 *
	 */
	public void setCreateUserId(Long createUserId) {
		this.createUserId = createUserId;
	}

	/**  
	 * createDateTime  
	 *  
	 * @return  the createDateTime 
	 *
	 */
	public Date getCreateDateTime() {
		return createDateTime;
	}

	/**  
	 * @param createDateTime the createDateTime to set  
	 *
	 */
	public void setCreateDateTime(Date createDateTime) {
		this.createDateTime = createDateTime;
	}

	/**  
	 * updateUserId  
	 *  
	 * @return  the updateUserId 
	 *
	 */
	public Long getUpdateUserId() {
		return updateUserId;
	}

	/**  
	 * @param updateUserId the updateUserId to set  
	 *
	 */
	public void setUpdateUserId(Long updateUserId) {
		this.updateUserId = updateUserId;
	}

	/**  
	 * updateDateTime  
	 *  
	 * @return  the updateDateTime 
	 *
	 */
	public Date getUpdateDateTime() {
		return updateDateTime;
	}

	/**  
	 * @param updateDateTime the updateDateTime to set  
	 *
	 */
	public void setUpdateDateTime(Date updateDateTime) {
		this.updateDateTime = updateDateTime;
	}

	/**  
	 * deleteFlag  
	 *  
	 * @return  the deleteFlag 
	 *
	 */
	public Boolean getDeleteFlag() {
		return deleteFlag;
	}

	/**  
	 * @param deleteFlag the deleteFlag to set  
	 *
	 */
	public void setDeleteFlag(Boolean deleteFlag) {
		this.deleteFlag = deleteFlag;
	}

	/**  
	 * cacheFlushable  
	 *  
	 * @return  the cacheFlushable 
	 *
	 */
	public boolean isCacheFlushable() {
		return cacheFlushable;
	}

	/**  
	 * @param cacheFlushable the cacheFlushable to set  
	 *
	 */
	public void setCacheFlushable(boolean cacheFlushable) {
		this.cacheFlushable = cacheFlushable;
	}
	
	/**  
	 * isUpdated  
	 *  
	 * @return  the isUpdated 
	 *
	 */
	public boolean isUpdated() {
		return isUpdated;
	}

	/**  
	 * @param isUpdated the isUpdated to set  
	 *
	 */
	public void setUpdated(boolean isUpdated) {
		this.isUpdated = isUpdated;
	}
	
	

	/**  
	 * isNew  
	 *  
	 * @return  the isNew 
	 *
	 */
	public boolean isNewWithId() {
		return isNewWithId;
	}

	/**  
	 * @param isNew the isNew to set  
	 *
	 */
	public void setNewWithId(boolean isNew) {
		this.isNewWithId = isNew;
	}
	
	/**  
	 * autoGenId  
	 *  
	 * @return  the autoGenId 
	 *
	 */
	public boolean isAutoGenId() {
		return autoGenId;
	}

	/**  
	 * @param autoGenId the autoGenId to set  
	 *
	 */
	public void setAutoGenId(boolean autoGenId) {
		this.autoGenId = autoGenId;
	}

	/**  
	 * convertDisUuid
	 * 将UUID 转化成带“-”的格式
	 *  
	 * @param uuid2
	 * @return
	 *   
	 * String  
	 * @exception   
	*/
	public static String convertDisUuid(String uuid) {
		if(uuid == null){
			return null;
		}
		StringBuffer buffer = new StringBuffer();
		if(!uuid.contains(UUID_SEPERATOR) && uuid.length() == 32){
			buffer.append(uuid.substring(0, 8))
				  .append(UUID_SEPERATOR)
				  .append(uuid.substring(8, 12))
				  .append(UUID_SEPERATOR)
				  .append(uuid.substring(12, 16))
				  .append(UUID_SEPERATOR)
				  .append(uuid.substring(16, 20))
				  .append(UUID_SEPERATOR)
				  .append(uuid.substring(20));
		}else{
			buffer.append(uuid);
		}
		return buffer.toString();
	}
	
	/**
	 * 
	 * setFiledValue
	 * @方法作用
	 * 设定该区域的值
	 *   
	 * void  
	 * @exception
	 */
	public void setFiledValue(String fieldName, int value){
		try {
			PropertyUtils.setProperty(this, fieldName, value);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
	}
	
	public void addFiledValue(String fieldName, int value){
		try {
			Integer oldVlaue = (Integer)PropertyUtils.getProperty(this, fieldName);
			PropertyUtils.setProperty(this, fieldName, oldVlaue + value);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public void addFiledValue(String fieldName, double value){
		try {
			double oldVlaue = (Double)PropertyUtils.getProperty(this, fieldName);
			PropertyUtils.setProperty(this, fieldName, oldVlaue + value);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public BigDecimal calRate(int data1, int data2, int scale){
		Double d1 = Double.valueOf(data1);
		Double d2 = Double.valueOf(data2);
		double divisionResult = 0;
		if(data2 != 0){
			divisionResult = d1 / d2;
		}
		BigDecimal result = new BigDecimal(divisionResult);
		
		return result.setScale(scale, BigDecimal.ROUND_HALF_EVEN);
	}
}
